#include "OpenGLBuffer.h"
#include "OpenGLUtils.h"
POCELAIN_NAMESPACE_BEGIN

size_t OpenGLBuffer::getitemSize() const
{
    return mItemSize;
}

void OpenGLBuffer::setItemSize(size_t newItemSize)
{
    mItemSize = newItemSize;
}

OpenGLBuffer::OpenGLBuffer()
{
    glGenBuffers(1, &mBufferName);
    POCELAIN_OPENGL_CHECK_ERROR("glGenBuffers");
    if(mBufferName==GL_INVALID_VALUE)
    {
        POCELAIN_LOG_ERROR("glGenBuffers error");
    }
}
OpenGLBuffer::~OpenGLBuffer()
{
    if(
            mBufferName!=GL_INVALID_VALUE&&
            glIsBuffer(mBufferName)==GL_TRUE)
    {
        glDeleteBuffers(1,&mBufferName);
        POCELAIN_OPENGL_CHECK_ERROR("glDeleteBuffers");
    }
}
void OpenGLBuffer::bind()
{
    glBindBuffer((GLenum)mTarget,mBufferName);
    POCELAIN_OPENGL_CHECK_ERROR("glBindBuffer");
}
void OpenGLBuffer::unbind()
{
    glBindBuffer((GLenum)mTarget,0);
    POCELAIN_OPENGL_CHECK_ERROR("glBindBuffer");
}
void OpenGLBuffer::write(const void *data, size_t byteCount, size_t offset /*= 0*/)
{
    bind();
    glBufferData(
        (GLenum)mTarget,
        byteCount,
        static_cast<const uint8_t*>(data) + offset,
        (GLuint)mUsage);
    POCELAIN_OPENGL_CHECK_ERROR("glBufferData");
}
POCELAIN_NAMESPACE_END
